package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.TagBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.TagDao;
import com.sp.scmdb.entity.Tag;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.TagService;

@Service("tagService")
public class TagServiceImpl implements TagService {

	private TagDao tagDao;
	
	/**日志service*/
	private LogService logs;
	
	/**
	 * @param logs the logs to set
	 */
	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}

	/**
	 * @param tagDao
	 *            the tagDao to set
	 */
	@Required
	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	@Transactional
	public String addTag(Tag t,UserBean ub) {

		String result = "3";
		StringBuffer Hql = new StringBuffer("from Tag where 1=1 ");
		if (StringUtils.isNotEmpty(t.getTagno())) {
			Hql.append("and tagno='").append(t.getTagno()).append("'");
			List<Tag> list = queryTagName(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				tagDao.save(t);
				/**添加日志*/
				logs.addLog(ub, "添加 卡号："+t.getTagno()+" 成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
			return result;
	}

	@Transactional
	public String delTag(String[] arr,UserBean ub) {
		String result = "3";
		List<Tag> tags = new ArrayList<Tag>();
		if (arr.length > 0) {
			for (int i = 0; i < arr.length; i++) {
				List<Tag> list = queryTagName(" from Tag where 1=1 and id = "+ arr[i]);
				Tag tag = list.get(0);
				// 1代表该卡号已经被使用不准删除！
				if (tag.getUsestat().equals("1")) {
					result = CommonParamter.CHECK_FAIL;
					return result;
				} else {
					tags.add(tag);
				}
			}
				//如果都是未使用的则全部删除！！！
			  for (int i = 0; i < tags.size(); i++) {
				Tag tag = tags.get(i);
				if (tag.getUsestat().equals("0")) {
					tagDao.delete(tag);
					/**添加日志*/
					logs.addLog(ub, "删除 卡号："+tag.getTagno()+" 成功！");
					result = CommonParamter.CHECK_SUCCESS;
				}
			}
		} else {
			result = CommonParamter.CHECK_FAIL;
		}
		return result;
	}

	@Transactional
	public String editTag(TagBean t, UserBean ub) {

		String result = "3";
		StringBuffer Hql = new StringBuffer("from Tag where 1=1 ");
		if (StringUtils.isNotEmpty(t.getTagno())) {
			Hql.append("and tagno='").append(t.getTagno()).append("'");
			List<Tag> list = queryTagName(Hql.toString());
			if (CollectionUtils.isNotEmpty(list)) {
				Tag tag = (Tag) list.get(0);
				tag.setTagno(t.getTagno());
				tag.setUsestat(t.getUsestat());
				tag.setPowerstat(t.getPowerstat());
				tagDao.saveOrUpdate(tag);
				/**添加日志*/
				logs.addLog(ub, "修改 卡号："+t.getTagno()+" 成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	public TagBean queryTagALL(TagBean newTagBean ,TagBean oldTagBean) {
		
		List tagList = new ArrayList();
		if (newTagBean.equals(oldTagBean)) {
			tagList = oldTagBean.getAllList();
		} else {
			String no = newTagBean.getTagno();
			String use=newTagBean.getUsestat();
			String power = newTagBean.getPowerstat();
			StringBuffer sb = new StringBuffer();
			sb.append("from Tag where 1=1");
			
			if (StringUtils.isNotEmpty(no)) {
				sb.append(" and  tagno like   '%" + no + "%' ");
			}
			if (StringUtils.isNotEmpty(use)) {
				sb.append("  and usestat = '" + use + "'");
			}
			if (StringUtils.isNotEmpty(power)) {
				sb.append("  and powerstat = '" + power + "'");
			}
			tagList = tagDao.queryHql(sb.toString());
		}
		newTagBean.setTagno(newTagBean.getTagno());
		if (CollectionUtils.isNotEmpty(tagList)) {
			newTagBean.setAllList(tagList);
			newTagBean.setList(tagList.subList(newTagBean.getCurrentRow(), newTagBean
					.getLastRow()));
		}
			return newTagBean;
	}

	public List<Tag> queryTagName(String Hql) {
		return tagDao.queryHql(Hql);
	}
}
